package com.bsoft.gol.editor.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import com.bsoft.gol.editor.editor.EditorUser;
import com.bsoft.gol.editor.editor.qo.EditorUserQo;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.transform.Transformers;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class EditorUserDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<EditorUser>{
    public EditorUserDAO() {
        this.setKeyField("loginUserId");
    }

    @DAOMethod(sql = "from EditorUser where loginName=:loginName")
    public abstract EditorUser getEditorUserByLoginName(@DAOParam("loginName") String loginName);


    @DAOMethod(sql = "from EditorUser where loginName=:loginName ")
    public abstract EditorUser getEditorUserByOgrId(@DAOParam("loginName") String loginName);




    @DAOMethod(sql = "from EditorUser where loginName=:loginName")
    public abstract List<EditorUser> queryEditorUserByLoginName(@DAOParam("loginName") String loginName);


    public  List<EditorUser> queryEditorUserList(final EditorUserQo req) {
        HibernateStatelessResultAction<List<EditorUser>> action = new AbstractHibernateStatelessResultAction<List<EditorUser>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();

                hsql.append("select login_user_id loginUserId,user_name userName,user_password passWord,org_id orgId,org_name orgName,user_type userType,login_name loginName,department_id departmentId,department_name departmentName,status status,create_time createTime,update_time updateTime from editor_user a where 1=1");
                if(CommonUtils.isNotNull(req.getLoginName())){
                    hsql.append(" and  login_name like :loginName");
                }
                if(CommonUtils.isNotNull(req.getOrgId())){
                    hsql.append(" and  org_id = :orgId");
                }
                if(CommonUtils.isNotNull(req.getStatus())){
                    hsql.append(" and  status = :status");
                }
                if(CommonUtils.isNotNull(req.getUserName())){
                    hsql.append(" and  user_name like :userName");
                }
                hsql.append("  order by  create_time desc ");

                SQLQuery sqlQuery = ss.createSQLQuery(hsql.toString());

                if(CommonUtils.isNotNull(req.getLoginName())){
                    sqlQuery.setParameter("loginName","%"+req.getLoginName()+"%");
                }
                if(CommonUtils.isNotNull(req.getOrgId())){
                    sqlQuery.setParameter("orgId",req.getOrgId());
                }
                if(CommonUtils.isNotNull(req.getStatus())){
                    sqlQuery.setParameter("status",req.getStatus());
                }
                if(CommonUtils.isNotNull(req.getUserName())){
                    sqlQuery.setParameter("userName","%"+req.getUserName()+"%");
                }

                sqlQuery = (SQLQuery) sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

                sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                //分页
                int first = req.getPageNo() > 0 ? (req.getPageNo() - 1) * req.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(req.getPageSize());

                List<Map> list = sqlQuery.list();
                List<EditorUser> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, EditorUser.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);

            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    public Long queryUserCount(final EditorUserQo req) {
        HibernateStatelessResultAction<Long> action = new AbstractHibernateStatelessResultAction<Long>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuilder hsql = new StringBuilder();
                hsql.append("select count(1) countValue from editor_user a where 1=1");
                if(CommonUtils.isNotNull(req.getLoginName())){
                    hsql.append(" and  login_name like :loginName");
                }
                if(CommonUtils.isNotNull(req.getOrgId())){
                    hsql.append(" and  org_id = :orgId");
                }
                if(CommonUtils.isNotNull(req.getStatus())){
                    hsql.append(" and  status = :status");
                }
                if(CommonUtils.isNotNull(req.getUserName())){
                    hsql.append(" and  user_name like :userName");
                }
                hsql.append("  order by  create_time desc ");
                Query sqlQuery = ss.createSQLQuery(hsql.toString());
                if(CommonUtils.isNotNull(req.getLoginName())){
                    sqlQuery.setParameter("loginName","%"+req.getLoginName()+"%");
                }
                if(CommonUtils.isNotNull(req.getOrgId())){
                    sqlQuery.setParameter("orgId",req.getOrgId());
                }
                if(CommonUtils.isNotNull(req.getStatus())){
                    sqlQuery.setParameter("status",req.getStatus());
                }
                if(CommonUtils.isNotNull(req.getUserName())){
                    sqlQuery.setParameter("userName","%"+req.getUserName()+"%");
                }

                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);

                List<Map> dbList = sqlQuery.list();

                if (CommonUtils.isNotEmpty(dbList)) {
                    setResult(Long.valueOf(String.valueOf(dbList.get(0).get("countValue"))));
                } else {
                    setResult(0L);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    public  List<EditorUser> queryEditorDeptList(final EditorUserQo req) {
        HibernateStatelessResultAction<List<EditorUser>> action = new AbstractHibernateStatelessResultAction<List<EditorUser>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();

                hsql.append("select org_id orgId,org_name orgName,department_id departmentId,department_name departmentName from editor_user a where 1=1");
                if(CommonUtils.isNotNull(req.getOrgId())){
                    hsql.append(" and  org_id =:orgId");
                    hsql.append("  group by  department_id  ");
                }else{
                    hsql.append("  group by  org_id,department_id  ");
                }



                SQLQuery sqlQuery = ss.createSQLQuery(hsql.toString());

                if(CommonUtils.isNotNull(req.getOrgId())){
                    sqlQuery.setParameter("orgId",req.getOrgId());
                }
                sqlQuery = (SQLQuery) sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

                sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                //分页
                int first = req.getPageNo() > 0 ? (req.getPageNo() - 1) * req.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(req.getPageSize());

                List<Map> list = sqlQuery.list();
                List<EditorUser> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, EditorUser.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);

            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }



    public Long queryDeptCount(final EditorUserQo req) {
        HibernateStatelessResultAction<Long> action = new AbstractHibernateStatelessResultAction<Long>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuilder hsql = new StringBuilder();
                hsql.append("select count(1) countValue from editor_user a where 1=1");
                if(CommonUtils.isNotNull(req.getOrgId())){
                    hsql.append(" and  org_id =:orgId");
                }
                hsql.append("  group by  department_id  ");
                Query sqlQuery = ss.createSQLQuery(hsql.toString());
                if(CommonUtils.isNotNull(req.getOrgId())){
                    sqlQuery.setParameter("orgId",req.getOrgId());
                }


                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);

                List<Map> dbList = sqlQuery.list();

                if (CommonUtils.isNotEmpty(dbList)) {
                    setResult(Long.valueOf(String.valueOf(dbList.get(0).get("countValue"))));
                } else {
                    setResult(0L);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }




}
